import json
import requests
from typing import *
from types import SimpleNamespace

base_url = 'http://223.0.30.25:1999/v1'
api_key = 'EMPTY'

client = requests.Session()
headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + api_key
}


def chat(messages: List[Dict[str, str]]):
    api_url = base_url + '/chat/completions'
    resp = client.post(api_url, headers=headers, json={
        "messages": messages,
        "model": "qwen-72b-chat",
        "temperature": 0.85,
        "top_p": 0.8
    })
    # 直接用 resp.json() 按字典取值也行，这是为了兼容 openai 写法
    completion = json.loads(resp.text, object_hook=lambda x: SimpleNamespace(**x))
    return completion.choices[0].message.content

def chat_with_prompt(prompt):
    message = [{'role': "user", "content": prompt}]
    result = chat(message)
    return result

def chat_t(messages: List[Dict[str, str]], temperature=0.85):
    api_url = base_url + '/chat/completions'
    resp = client.post(api_url, headers=headers, json={
        "messages": messages,
        "model": "qwen-72b-chat",
        "temperature": temperature,
        "top_p": 0.8
    })
    # 直接用 resp.json() 按字典取值也行，这是为了兼容 openai 写法
    completion = json.loads(resp.text, object_hook=lambda x: SimpleNamespace(**x))
    return completion.choices[0].message.content

def chat_with_prompt_t(prompt, temperature=0.85):
    message = [{'role': "user", "content": prompt}]
    result = chat_t(message, temperature)
    return result

if __name__ == '__main__':
    content = """
    我想请你扮演一个翻译专家, 你的工作是把下面 triple backticks 中的中文翻译成英文.

    ```
    你好啊! 很高兴见到你!
    ```
    """
    # message = [{'role': "user", "content": content}]

    # result = chat(message)
    result = chat_with_prompt_t(content, 0)

    print(result)